package com.mairuide.inspectionReport;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.base.ModuleCode;
import com.mairuide._frame.utils.GsonUtil;
import com.mairuide.datalog.*;
import com.mairuide.entity.PageResult;
import com.mairuide.inspectionReport.utils.UnZipAnRar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zyj
 * @Date: 2019/3/20 11:23
 * @Description:
 */
@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {

    @Autowired
    private ReportService reportService;

    @Resource
    private DataOperationLogService dataOperationLogService;

    @RequestMapping(value = "/showUpload",method = RequestMethod.GET)
    public Object transformation(ModelMap model){
        return "/report/importReport";
    }
    @ModuleCode(pValue = "ElectronicsReport", value = "ElectronicsReport")
    @RequestMapping(value = "/reportList",method = RequestMethod.GET)
    public Object reportList(ModelMap model){
        return "/report/importList";
    }


    @RequestMapping("/uploadFile")
    @ResponseBody
    public void uploadFile(@RequestParam("fileUpload") MultipartFile file, String caseId,Integer reportType,
                           HttpServletResponse response){

        if (reportType<0) {
            try {
                response.getWriter().print(GsonUtil.GsonString(ERROR("请选择取证类型")));
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        List<DataOperationImportLog> dils = new ArrayList<DataOperationImportLog>();
        try {
            DataOperationImportLog report = super.reportFile(file);
            report.setUserId(super.getLoginUser().getId());
            report.setIsDelete(0);
            report.setTableFlat(3);
            report.setReportType(reportType);
            report.setCreateTime(new Timestamp(System.currentTimeMillis()));
            report.setUnSuccess(0);
            String filename = file.getOriginalFilename();
            if (filename.endsWith(".zip")){
                UnZipAnRar.unZip(report.getFilePath(),report.getFilePath().substring(0,report.getFilePath().lastIndexOf("\\")+1));
                report.setFilePath(report.getFilePath().substring(0,report.getFilePath().lastIndexOf(".")));
            }else if (filename.endsWith(".rar")){
                UnZipAnRar.unRar(report.getFilePath(),report.getFilePath().substring(0,report.getFilePath().lastIndexOf("\\")+1));
                report.setFilePath(report.getFilePath().substring(0,report.getFilePath().lastIndexOf(".")));
            }
            String substring = filename.substring(0, filename.lastIndexOf("."));
            report.setFileName(substring);
            report.setUnSuccess(1);
            dils.add(report);
            switch (reportType){
                case 0:{
                    reportService.extractionMeiYa(substring,caseId,super.getLoginUser().getId());
                    break;
                }case 1:{

                }
            }

            response.getWriter().print(GsonUtil.GsonString(SUCCESS("上传成功")));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                dataOperationLogService.save(1, dils, super.getLoginUser(), SystemFlag.DZ,
                        ModuleFlag.BILLINPUT, ActionType.INPUT, caseId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/queryReport")
    @ResponseBody
    public Object reportList(String caseId,Integer page,Integer rows){
        PageResult pageMap = reportService.findByCasemanageId(caseId, page, rows);
        return pageMap;
    }

}
